<%@page import="com.jfinal.kit.PropKit"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	import="cn.com.dashihui.web.common.OrderCode" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<title>订单管理</title>
<jsp:include page="../../include/header.jsp"></jsp:include>
<style type="text/css">
table th {
	text-align: center;
	white-space: nowrap;
}

table td {
	text-align: center;
	white-space: nowrap;
}

table {
	empty-cells: show;
	border-collapse: collapse;
	margin: 0 auto;
}
/*排序 */
.querySort {
	position: relative;
}

.down {
	position: absolute;
	top: 12px;
	display: inline-block;
	width: 10px;
	height: 12px;
	margin-left: 8px;
	background: url("${BASE_PATH}/static/images/sprite-arrow.png") 0 61px;
}

.sort .querySort:NTH-OF-TYPE(2) .down {
	top: -3px;
	transform: rotateX(180deg);
}

.querySort:hover .down {
	background: url("${BASE_PATH}/static/images/sprite-arrow.png") 0 21px;
}

.downSort {
	position: absolute;
	top: 3px;
	left: 3px;
	width: 18px;
	height: 24px;
	opacity: 0;
	cursor: pointer;
}

.upSort {
	position: absolute;
	top: -15px;
	left: 3px;
	width: 18px;
	height: 24px;
	opacity: 0;
	cursor: pointer;
}
</style>
</head>
<body>
	<div id="wrapper" class="row">
		<div class="col-lg-12">
			<h1 class="page-header">订单统计</h1>
		</div>
		<!-- 搜索框 -->
		<div class="col-lg-12 title-top">
			<label class="search-label">订单编号：</label><input type="text"
				id="sOrderNum" value="" class="form-control search-input width125"
				maxlength="21"> <label class="search-label">买家电话：</label><input
				type="text" id="sTel" value=""
				class="form-control search-input width125" maxlength="13"> <label
				class="search-label">订单状态：</label>
			<div class="search-input">
				<select id="sState" class="selectpicker" data-width="100">
					<option value="0">全部</option>
					<option value="1">待付款</option>
					<option value="2">待出库</option>
					<option value="3">待评价</option>
					<option value="4">已完成</option>
					<option value="5">已删除</option>
					<option value="6">待收货</option>
					<option value="7">已退款</option>
					<option value="9">已过期</option>
				</select>
			</div>
			<c:if test="${RoleId==1||RoleId==2}">
				<label class="search-label">选择分公司：</label>
				<select id=sStoreId class="selectpicker pull-left p-l-5" data-width="130">
					<option value="">请选择分公司</option>
					<c:forEach items="${list}" var="Store">
						<option value="${Store.storeid}">${Store.title }</option>
					</c:forEach>
				</select>
			</c:if>
			<label class="search-label">下单时间：</label><input type="text"
				id="sBeginDate" autocomplete="off" value=""
				class="form-control search-input width100 datetimepicker"
				data-format="yyyy-mm-dd" > <label
				class="search-label">至</label><input type="text" id="sEndDate"
				autocomplete="off" value=""
				class="form-control search-input width100 datetimepicker"
				data-format="yyyy-mm-dd" >

			<div style="display: inline-block;">
				<button class="btn btn-success search-btn" onclick="query();">确定</button>
				<button class="btn btn-success search-btn" onclick="reset();">重置</button>
				<button class="btn btn-default search-btn" title="刷新" onclick="window.location.reload();"> 刷新</button>
			</div>
			<button onclick="doExcel();" class="btn btn-success pull-right">导出</button>
			<div class="clearfix"></div>
		</div>
		<div class="col-lg-12">
			<div class="col-lg-12">
					<div class="text-muted single-line-text pull-left">共 <font color="#428bca" id="dataCount">0</font>条记录</div>
					<div class="text-muted single-line-text pull-left">
						统计:
						<span class="m-l-5"> 
							总累计现金<font color="#428bca" id="amount">0.00</font>元
						</span>
						<span class="m-l-5"> 
							总退款现金<font color="#428bca" id="tuikuan">0.00</font>元
						</span>
						<span class="m-l-5"> 
							总实际现金收入<font color="#428bca" id="shiji">0.00</font>元
						</span>
					</div>
				</div>
			<!-- table-responsive表格的下编框的滚动条 -->
			<div class="table-responsive panel panel-default ">
				<table id="dataTable"
					class="table  table-hover table-noellipsis table-bordered"
					style="overflow: scroll;">
					<thead>
						<tr>
							<th width="100px;">状态</th>
							<th width="140px;">退款</th>
							<th width="200px;">订单号</th>
							<th width="80px;">会员姓名</th>
							<th width="150px;">会员卡号</th>
							<th width="120px;">联系方式</th>
							<th width="100px;">下单分公司</th>
							<th width="200px;">下单时间</th>
							<th width="80px;">订单总价值</th>
							<th width="200px;">支付时间</th>
							<th width="100px;">拓客总积分</th>
							<th width="100px;">拓客总抵扣券</th>
							<th width="100px;">拓客总惠券</th>
							<th width="100px;">营业部</th>
							
						</tr>
					</thead>
					<tbody id="dataList"></tbody>
				</table>
			</div>
			<div class="col-lg-12">
				<ul id="dataPagination" class="pagination-sm pull-right"></ul>
			</div>
		</div>
	</div>
	<jsp:include page="../../include/javascripts.jsp"></jsp:include>
	<!-- 异步加载下一页数据后，用模板渲染 -->
	<script type="text/html" id="dataTpl">
	   {{each list as item}}
				 <tr id="item{{item.orderNum}}" data-id="{{item.orderNum}}">
					{{if item.orderState == 1}}
						<td>待付款</td>
						{{else if item.orderState == 2}}
						<td>
							<button type="button" class="btn " onclick="toAddWaybill('{{item.orderNum}}')" title="待出库"><font color="#EE2C2C" ="dataCount">待出库</font></button>
						</td>
						{{else if item.orderState == 3}}
						<td>待评价</td>
						{{else if item.orderState == 4}}
						<td>已完成</td>
						{{else if item.orderState == 5}}
						<td>删除</td>	
						{{else if item.orderState == 7}}
							<td>退款完成</td>
						{{else if item.orderState == 9}}
							<td>过期订单</td>	
						{{else if item.orderState == 6}}
						<td>	
						<button type="button" class="btn " onclick="WBDetail('{{item.orderNum}}')" title="待收货"><font color="#40a9ff" ="dataCount">待收货</font></button>
						</td>		
					{{/if}}
					{{if item.tuistate == 0}}
							<td>正常</td>
						{{else if item.tuistate == 1}}
						<td>
							<a href="javascript:doAccept('{{item.orderNum}}');"><span class="btn">通过</span></a>
							<a href="javascript:toCancel('{{item.orderNum}}');"><span class="btn">驳回</span></a>
						</td>
						{{else if item.tuistate == 2}}
							<td>已通过</td>
						{{else if item.tuistate == 3}}
							<td>已驳回</td>
						{{else if item.tuistate == 4}}
							<td>已退款</td>	
						{{else if item.tuistate == 5}}
							<td>退款异常</td>	
					{{/if}}
					<td><a href="javascript:showOrder('{{item.orderNum}}');"><font color="#40a9ff" ="dataCount">{{item.orderNum}}</font></a></td>
					<td><a href="javascript:showUser('{{item.uid}}');"><font color="#40a9ff" id="dataCount">{{item.name}}</font></a></td>
                    <td>{{item.card}}</td>
					<td>{{item.tel}}</td>
                    <td>{{item.xdstore}}
						{{if !item.xdstore}}--{{/if}}
					</td>
					<td>{{item.startDate}}</td>
					<td>{{item.amount}}</td>

					<td>{{item.payDate}}
						{{if !item.payDate}}此订单尚未支付{{/if}}
					</td>
					<td>{{if item.totalPoint}}<a href="javascript:showToker('{{item.tokerid}}');"><font color="#40a9ff" ="dataCount">{{item.totalPoint}}</font></a>{{/if}}
						{{if !item.totalPoint}}--{{/if}}
					</td>
					<td>{{if item.totalDetion}}<a href="javascript:showToker('{{item.tokerid}}');"><font color="#40a9ff" ="dataCount">{{item.totalDetion}}</font></a>{{/if}}
						{{if !item.totalDetion}}--{{/if}}
					</td>
					<td>{{if item.totalDcount}}<a href="javascript:showToker('{{item.tokerid}}');"><font color="#40a9ff" ="dataCount">{{item.totalDcount}}</font></a>{{/if}}
						{{if !item.totalDcount}}--{{/if}}
					</td>
					<td>{{item.jjrName}}
						{{if !item.jjrName}}--{{/if}}
					</td>
				</tr>
		{{/each}}
</script>
	<script type="text/javascript">
var dataPaginator;
var userId="${userId}";
$(query);
function query(){
	var pointType=$("#pointType").val(),sStoreId = $("#sStoreId").val();
	
	var params = {
		pageSize:10,
		orderNum:$("#sOrderNum").val(),
		beginDate:$("#sBeginDate").val(),
		endDate:$("#sEndDate").val(),
		tel:$("#sTel").val(),
		state:$("#sState").val(),
		sType:$("#sType").val(),
		sStoreId:sStoreId,
		pointType:pointType
	};
	if(dataPaginator){
		dataPaginator.destroy();
	}
	if(!userId){
	dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/page",params,function(result){
		//设置显示最新的数据数量
		$("#dataCount").html(result.object.totalRow);
		//根据模板渲染数据并填充
		$("#dataList").empty().append(template("dataTpl",result.object));
		findTotal();
	});
	}else{
		console.log("ddddddddddd");
		//根据用户Id查询此用户所有订单
		dataPaginator = Kit.pagination("#dataPagination","${BASE_PATH}/bus/order/doConvert",{userId:userId},function(result){
			console.log("result.object"+result.object);
			//设置显示最新的数据数量
			$("#dataCount").html(result.object.totalRow);
			//根据模板渲染数据并填充
			$("#dataList").empty().append(template("dataTpl",result.object));
			findTotal(userId);
		});
		
	}
}
/**
 * 统计总所需积分和总现金价格
 */
function findTotal(){
var pointType=$("#pointType").val(),sStoreId = $("#sStoreId").val();
	
	var params = {
		pageSize:10,
		orderNum:$("#sOrderNum").val(),
		beginDate:$("#sBeginDate").val(),
		endDate:$("#sEndDate").val(),
		tel:$("#sTel").val(),
		state:$("#sState").val(),
		sType:$("#sType").val(),
		sStoreId:sStoreId,
		pointType:pointType
	};
	$.post("${BASE_PATH}/bus/order/findTotal",params,function(result){
		if(result.flag==0){
			//$("#payPoint").text(result.object.payPoint);//总所需积分
			$("#amount").text(result.object.amount.toFixed(2));//总累计现金
			$("#tuikuan").text(result.object.tuikuan1.toFixed(2));//总退款现金
			$("#shiji").text(result.object.shiji.toFixed(2));//总实际现金
		}else{
			Kit.alert("操作失败");return;
		}
	});
}
//显示会员详细信息
function showUser(id){
	if(id){
	showDialog = Kit.dialog("会员详情信息","${BASE_PATH}/bus/order/userdetail/"+id,{size:'size-wide',closable:true}).open();
	}else{
		//根据用户Id查询此用户所有订单
		showDialog = Kit.dialog("会员详情信息","${BASE_PATH}/bus/order/userdetail/"+userId,{size:'size-wide',closable:true}).open();
	}

}
//显示订单详细信息
var showOrderDialog;
function showOrder(orderNum){
	showOrderDialog = Kit.dialog("查看订单","${BASE_PATH}/bus/order/detail?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
//物流信息
var toAddWaybillDialog;
function toAddWaybill(orderNum){
	toAddWaybillDialog = Kit.dialog("添加订单物流信息","${BASE_PATH}/bus/order/toAddWaybill?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
//物流详情
var WBDetailDialog;
function WBDetail(orderNum){
	WBDetailDialog = Kit.dialog("添加订单物流信息","${BASE_PATH}/bus/order/WBDetail?orderNum="+orderNum,{size:'size-wide',closable:true}).open();
}
function onEditSuccess(newObject){
	$("#item"+newObject.id).replaceWith(template("dataTpl",{"list":[newObject]}));
} 
//通过退款申请
function doAccept(orderNum){
	Kit.confirm("提示","确定要审核退款申请吗？",function(ref){
		$.post("${BASE_PATH}/bus/order/doAccept",{'orderNum':orderNum},function(result){
			if(result.flag==0){
				onEditSuccess(result.object);
			}else if(result.flag==1){
				Kit.alert("重复操作");return;
			}else{
				Kit.alert("操作失败");return;
			}
		});
	});
}
//驳回申请
var toCancelDialog;
function toCancel(orderNum){
	toCancelDialog = Kit.dialog("驳回原因","${BASE_PATH}/bus/order/toRefound?orderNum="+orderNum,{closable:true,size:"size-wide:100px"}).open();
}
//显示拓客详细信息
var showTokerDialog;
function showToker(tokerid){
	showTokerDialog = Kit.dialog("查看拓客信息","${BASE_PATH}/bus/order/showToker?tokerid="+tokerid,{size:'size-wide',closable:true}).open();
}
$("#sSellerId").change(function(){
	var sSellerId = $("#sSellerId").val();
	if(sSellerId !=0){
	$.post("${BASE_PATH}/bus/goods/base/chooseStore",{sSellerId:sSellerId},function(result){
		var newarray = result.object;
			 for(var i=0; i<newarray.length; i++){  
				 $("#sStoreId").append ( "<option value= '"+newarray[i].storeid+"'>"+newarray[i].title+"</option>");  
            } 
           
		});
	}
});

function doExcel(){
	
     	var orderNum=$("#sOrderNum").val(),beginDate = $("#sBeginDate").val(),endDate = $("#sEndDate").val(), tel = $("#sTel").val(),state = $("#sState").val(),sStoreId = $("#sStoreId").val();
    	
    	if(!userId){
     	 window.location.href="${BASE_PATH}/bus/order/doExcel?orderNum="+orderNum+"&beginDate="+beginDate+"&endDate="+endDate+"&tel="+tel+"&state="+state+"&sStoreId="+sStoreId;
    	}else{
    		//根据用户ID导出
    		window.location.href="${BASE_PATH}/bus/order/doConvertExcel?userId="+userId;

    	}
	
	
	};
	$("#sBeginDate").datetimepicker({
	    format: 'yyyy-mm-dd hh:ii:ss',
	    minuteStep:1,
	    minView:'hour',
	    language: 'zh-CN',
	    pickerPosition:'bottom-right',
	    autoclose:true,
	}).on("click",function(){
		var d2=new Date();
		var d=$("#sEndDate").val();
		if(d==""){
			 $("#sBeginDate").datetimepicker("setEndDate",d2);
		}else{
			var d1=new Date(d); 
			d1.setDate(d1.getDate()-1);
			if(d1>d2){
		  	 	 $("#sBeginDate").datetimepicker("setEndDate",d2);
			}else{
				 $("#sBeginDate").datetimepicker("setEndDate",d1);
			}
		}
	});

	$("#sEndDate").datetimepicker({
	    format: 'yyyy-mm-dd hh:ii:ss',
	    minuteStep:1,
	    minView:'hour',
	    language: 'zh-CN',
	    autoclose:true,
		}).on("click",function(){
			var d=$("#sBeginDate").val();
			var d1=new Date(d); 
			d1.setDate(d1.getDate()+1); 
			$("#sEndDate").datetimepicker("setStartDate",d1);
		});
		
	 $(document).on("click","#sEndDate",function(){
	    	$('#sEndDate').datetimepicker('show');
	    });

	$(document).on("click","#sBeginDate",function(){
		$('#sBeginDate').datetimepicker('show');
	});
	function reset(){
		$("#sOrderNum").val("");$("#sBeginDate").val("");$("#sEndDate").val("");$("#sTel").val("");
		$("#sSellerId").val("");$("#sSellerId").trigger("change");  $("#sStoreId").val("");$("#sStoreId").trigger("change");  $("#sState").val("0");$("#sState").trigger("change");
		$("#sStoreId").selectpicker("refresh"); $("#sSellerId").selectpicker("refresh");
		$("#sType").val("0");$("#sType").selectpicker("refresh");
	}
</script>
</body>
</html>